# 不锈钢304/2B
import re
import time
import common.dataBase as db

import parsel
import requests


def get_data(url):
    response = requests.get(url)
    selector = parsel.Selector(response.text)
    urls = selector.css(
        'body > div.wrap.noMtop > div.rightBar.noBor > div.scroll-fixed > div:nth-child(1) > ul > li > a')
    url1 = ''
    for url in urls:
        title = url.xpath('.//text()').get()
        if re.search('佛山', title):
            url1 = url.xpath('.//@href').get()
            print(url1)

    response1 = requests.get(url1)
    selector1 = parsel.Selector(response1.text)
    # # #text > table > tbody > tr:nth-child(2) > td:nth-child(6)
    return selector1.css('#text table tr:nth-child(2) td:nth-child(2)::text').get()


def app():
    url = 'https://list.info.mybxg.com/price/p-10241--01021601--1.html'
    today = time.strftime('%Y-%m-%d', time.localtime())
    name = '201/2B不锈钢'
    price_range = ''
    price = get_data(url)
    insert_sql = f'INSERT INTO mat_price VALUES (null, "{name}", "{price}","{price_range}", "{today}", "{url}")'
    print(insert_sql)
    db.save([insert_sql])


if __name__ == '__main__':
    app()
